package com.keon.jianbao.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.enu.MainTab;
import com.keon.jianbao.enu.WebType;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener {

    @Bind(android.R.id.tabhost)
    public FragmentTabHost mTabHost;

    private long exitTime = 0;
    String tag = "";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.main;
    }

    @Override
    protected boolean hasBackButton() {
        return false;
    }

    @Override
    public void initView() {
        initTabs();
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tweet_title_new:
//                showToast(R.string.abc_action_bar_home_description);
                jump(TweetPublishActivity.class);
            case R.id.toolbar_title_tab1:
                chooseToolbarTab(id);
                break;
            case R.id.toolbar_title_tab2:
                chooseToolbarTab(id);
                break;
            case R.id.main_activity_area:

                break;
        }
    }

    private void changeTitle(String s) {
        showToolBar();
        removeRightView();
        removeLeftView();
        removeToolBarTab();
        setTitle(s);

        if (s == getString(R.string.main_tab_friends)) {
//            setRightView(R.layout.view_icon_publish_tweet);
//            setToolBarTab(R.string.activity_tab_platform, R.string.activity_tab_personal);
            hideToolBar();
        } else if (s == getString(R.string.main_tab_activity)) {
       /*     setToolBarTab(R.string.activity_tab_platform, R.string.activity_tab_personal);
            setRightView(R.layout.view_icon_publish_activity);
            setLeftView(R.layout.view_menu_area);*/
            hideToolBar();
        } else if (s == getString(R.string.main_tab_me)) {
            hideToolBar();
        } else {
        }
    }

    private void initTabs() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        //得到fragment的个数

        MainTab[] tabs = MainTab.values();
        int count = tabs.length;

        for (int i = 0; i < count; i++) {
            MainTab mainTab = tabs[i];
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(mainTab.getResName())).setIndicator(getTabItemView(mainTab));
            //将Tab按钮添加进Tab选项卡中
            if (mainTab.getIdx() == MainTab.ACTIVITY.getIdx()) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", WebType.ACTIVITYLIST.getId());
                mTabHost.addTab(tabSpec, mainTab.getClz(), bundle);
            } else {
                mTabHost.addTab(tabSpec, mainTab.getClz(), null);
            }

            //设置Tab按钮的背景
//            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);

            mTabHost.setCurrentTab(0);
            mTabHost.setOnTabChangedListener(this);
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(MainTab mainTab) {
        View indicator = LayoutInflater.from(getApplicationContext())
                .inflate(R.layout.tab_indicator, null);
        TextView title = (TextView) indicator.findViewById(R.id.tab_title);
        title.setText(getString(mainTab.getResName()));
        Drawable drawable = this.getResources().getDrawable(
                mainTab.getResIcon());
        title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                null);

        return indicator;
    }

    @Override
    public void onTabChanged(String s) {
        changeTitle(s);
        tag = s;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    AppManager.getAppManager().AppExit(MainActivity.this);
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
